<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="受益人姓名" prop="syName">
        <el-input
          v-model="queryParams.syName"
          placeholder="请输入受益人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span>{{ statusDict[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="受益人姓名" align="center" prop="syName" />
      <el-table-column label="被保证人姓名" align="center" prop="bbzrName" />
      <el-table-column label="保函类型" align="center" prop="bhType" />
      <el-table-column label="保函金额" align="center" prop="bhMoney" />
      <el-table-column label="项目名称" align="center" prop="xmName" />
      <el-table-column label="担保期限" align="center" prop="dbQixian" />
      <el-table-column label="中标时间或合同签署时间" align="center" prop="xmDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工期" align="center" prop="xmGongqi" />
      <el-table-column label="出函银行" align="center" prop="xmChBank" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 2"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >审核</el-button>
          <el-button v-if="scope.row.status != 0" size="mini" type="text" icon="el-icon-copy-document"
            @click="handleCopy(scope.row)">一键复制</el-button>
          <el-button v-if="scope.row.status != 2" size="mini" type="text" icon="el-icon-tickets" @click="openDetail(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改询单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1360px" append-to-body>
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="180px">
        <el-form-item label="受益人姓名" prop="syName">
          <div class="label_txt">{{ form.syName }}</div>
        </el-form-item>
        <el-form-item label="受益人企业性质" prop="syQyxz">
          <div class="label_txt">{{ form.syQyxz }}</div>
        </el-form-item>
        <el-form-item label="被保证人姓名" prop="bbzrName">
          <div class="label_txt">{{ form.bbzrName }}</div>
        </el-form-item>
        <el-form-item label="被保证人企业性质" prop="bbzrQyxz">
          <div class="label_txt">{{ form.bbzrQyxz }}</div>
        </el-form-item>
        <el-form-item label="被保证人注册资金" prop="bbzrZczj">
          <div class="label_txt">{{ form.bbzrZczj }}</div>
        </el-form-item>
        <el-form-item label="被保证人成立时间" prop="bbzrDate">
          <div class="label_txt">{{ form.bbzrDate }}</div>
        </el-form-item>
        <el-form-item label="对应资质" prop="dyzz">
          <div class="label_txt">{{ form.dyzz }}</div>
        </el-form-item>
        <el-form-item label="最高资质" prop="zgzz">
          <div class="label_txt">{{ form.zgzz }}</div>
        </el-form-item>
        <el-form-item label="保函格式" prop="bhGs">
          <div class="label_txt">{{ form.bhGs }}</div>
        </el-form-item>
        <el-form-item label="保函金额" prop="bhMoney">
          <div class="label_txt">{{ form.bhMoney }}</div>
        </el-form-item>
        <el-form-item label="项目名称" prop="xmName">
          <div class="label_txt">{{ form.xmName }}</div>
        </el-form-item>
        <el-form-item label="担保期限" prop="dbQixian">
          <div class="label_txt">{{ form.dbQixian }}</div>
        </el-form-item>
        <el-form-item label="中标时间" prop="xmDate">
          <div class="label_txt">{{ form.xmDate }}</div>
        </el-form-item>
        <el-form-item label="工期" prop="xmGongqi">
          <div class="label_txt">{{ form.xmGongqi }}</div>
        </el-form-item>
        <el-form-item label="出函银行" prop="xmChBank">
          <div class="label_txt">{{ form.xmChBank }}</div>
        </el-form-item>
        <el-form-item label="其它备注" prop="remark">
          <div class="label_txt">{{ form.remark }}</div>
        </el-form-item>
        <!-- 审核信息 -->
                <el-divider content-position="left">审核信息</el-divider>
                <div>
                  <el-form-item label="审核状态:">
                    <el-radio-group  prop v-model="form.status">
                      <el-radio :label="4">通过</el-radio>
                      <el-radio :label="-1">不通过</el-radio>
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item label="审核意见" prop="checkMind" :rules="{ required: true, message: '请输入', trigger: 'blur' }">
                    <el-input v-model="form.checkMind" placeholder="请输入审核意见" />
                  </el-form-item>
                </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情弹窗 -->
    <order-detail :form="detailform" :detailOpen="detailOpen" @changeDialog="changeDialog" />
    <!-- 一键复制弹窗 -->
    <el-dialog title="一键复制" :visible.sync="copyVisible" width="30%" :before-close="copyClose">
      <div v-if="copyDetail">
        <div class="copyTxt" v-for="(value, key, index) in copyDetail" :key="index">
          {{ key }}: {{ value }}
        </div>
      </div>
      <div v-else="copyDetail">暂无可复制信息</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="copyVisible = false">取 消</el-button>
        <el-button v-if="copyDetail" type="primary" v-clipboard:copy="copyText()" v-clipboard:success="clipboardSuccess"
                   @click="copyVisible = false">复 制</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckOrder, getOrder, tijiao, delOrder, addOrder, checkOrder ,zhuanOrder,copyAgreement} from "@/api/guarantee/order";
import OrderDetail from '@/views/guarantee/order/orderDetail'

export default {
  name: "Order",
  components: {
    OrderDetail
  },
  data() {
    return {
      statusDict: {
        '0': '草稿',
        '2': '待审核',
        '4': '完成',
        '6': '已转入',
        '-1': ' 驳回'
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 询单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      // 一键复制
      copyVisible: false,
      copyDetail: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        syName: null,
        syQyxz: null,
        bbzrName: null,
        bbzrQyxz: null,
        bbzrZczj: null,
        bbzrDate: null,
        dyzz: null,
        zgzz: null,
        bhType: null,
        bhGs: null,
        bhMoney: null,
        xmName: null,
        dbQixian: null,
        xmDate: null,
        xmGongqi: null,
        xmChBank: null,
        checkUserId: null,
        checkTime: null,
        checkMind: null,
      },
      // 表单参数
      form: {},
      // 详情弹窗
      detailOpen: false,
      detailform: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询询单列表 */
    getList() {
      this.loading = true;
      listCheckOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        syName: null,
        syQyxz: null,
        bbzrName: null,
        bbzrQyxz: null,
        bbzrZczj: null,
        bbzrDate: null,
        dyzz: null,
        zgzz: null,
        bhType: null,
        bhGs: null,
        bhMoney: null,
        xmName: null,
        dbQixian: null,
        xmDate: null,
        xmGongqi: null,
        xmChBank: null,
        remark: null,
        checkUserId: null,
        checkTime: null,
        checkMind: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加询单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改询单";
      });
    },
    /** 转入按钮操作 */
    handleZhuan(row) {
      this.reset();
      const id = row.id;
      this.$modal.confirm('是否确认转入项目为"' + row.xmName + '"的数据项？').then(function() {
        return zhuanOrder(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("转入成功");
      }).catch(() => {});
    },
    /** 详情按钮操作 */
    openDetail(row) {
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.detailform = response.data;
        this.detailOpen = true;
      });
    },
    changeDialog(val) {
      this.detailOpen = val;
    },
    /** 提交按钮操作 */
    handleTijiao(row) {
      this.$modal.confirm('是否确认提交履约保函项目为为"' + row.xmName + '"的数据项？').then(function () {
        return tijiao(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => { });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            checkOrder(this.form).then(response => {
              this.$modal.msgSuccess("审核修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 一键复制 */
    handleCopy(row) {
      const id = row.id || this.ids
      this.copyDetail = {}
      copyAgreement(id).then((res) => {
        this.copyDetail = res.data;
      })
      this.copyVisible = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除询单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 格式化复制文本内容 */
    copyText() {
      if (this.copyDetail) {
        let text = '';
        for (let key in this.copyDetail) {
          text += key + ':' + this.copyDetail[key] + '\n'
        }
        return text;
      }
    },
    /** 复制代码成功 */
    clipboardSuccess() {
      this.$modal.msgSuccess("复制成功");
    },
    /** 一键复制 */
    copyClose() {
      this.copyVisible = false
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('guarantee/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.my_select {
  width: 204px;
}

.my_picker {
  width: 204px;
}

.lotInput {
  width: 150px;
  margin-right: 10px;
}

.bd_box {
  display: flex;
  flex-wrap: wrap;
}

.divider_tit {
  margin-right: 10px;
}

.b_divider_tit {
  margin-bottom: 50px;
}

.s_divider_tit {
  width: 94%;
  margin: 24px 20px;
}

.disable_upload ::v-deep .el-upload--picture-card,.disable_upload ::v-deep .upload-file-uploader, .disable_upload::v-deep .ele-upload-list__item-content-action{
  display: none
}
.label_txt {
  width: 204px
}
</style>
